'''用id进行去重'''
# class UniquePathContentRecorder:
#     def __init__(self, max_count=10000):
#         self.max_count = max_count
#         self.path_map = {}  # 路径 -> 内容
#         self.order = []     # 保持顺序
#
#     def add(self, path, content):
#         """
#         添加路径及内容，若重复立即打印，若成功添加返回 True。
#         """
#         if path in self.path_map:
#             print(f"⚠️ 重复路径: {path}，已存在内容: {self.path_map[path]}，新内容: {content}")
#             return False
#         if len(self.order) >= self.max_count:
#             print(f"⚠️ 超过最大记录数 {self.max_count}，未添加: {path}")
#             return False
#         self.path_map[path] = content
#         self.order.append(path)
#         return True
#
#     def get_all(self):
#         return [{ "path": p, "content": self.path_map[p] } for p in self.order]
#
#
# if __name__ == "__main__":
#     recorder = UniquePathContentRecorder()
#     print("请输入路径和内容（输入 q 退出）：")
#     while True:
#         path = input("路径: ").strip()
#         if path.lower() == 'q':
#             break
#         content = input("内容: ").strip()
#         recorder.add(path, content)
#     print("\n✅ 最终唯一记录：")
#     for i, item in enumerate(recorder.get_all(), 1):
#         print(f"{i}. 路径: {item['path']}，内容: {item['content']}")

'''用内容去重'''
# class UniqueContentRecorder:
#     def __init__(self, max_count=10000):
#         self.max_count = max_count
#         self.content_map = {}  # 内容 -> 路径（或其他信息）
#         self.order = []        # 保持顺序
#
#     def add(self, path, content):
#         """
#         添加路径及内容，若内容重复立即打印，若成功添加返回 True。
#         """
#         if content in self.content_map:
#             print(f"⚠️ 重复内容: {content}，已存在路径: {self.content_map[content]}，新路径: {path}")
#             return False
#         if len(self.order) >= self.max_count:
#             print(f"⚠️ 超过最大记录数 {self.max_count}，未添加: {path}")
#             return False
#         self.content_map[content] = path
#         self.order.append(content)
#         return True
#
#     def get_all(self):
#         return [{ "path": self.content_map[c], "content": c } for c in self.order]
#
#
# if __name__ == "__main__":
#     recorder = UniqueContentRecorder()
#     print("请输入路径和内容（输入 q 退出）：")
#     while True:
#         path = input("路径: ").strip()
#         if path.lower() == 'q':
#             break
#         content = input("内容: ").strip()
#         recorder.add(path, content)
#     print("\n✅ 最终唯一记录（按内容去重）：")
#     for i, item in enumerate(recorder.get_all(), 1):
#         print(f"{i}. 路径: {item['path']}，内容: {item['content']}")

'''计算相似性'''
# import difflib
#
# class UniqueContentRecorder:
#     def __init__(self, max_count=10000, similarity_threshold=0.9):
#         self.max_count = max_count
#         self.similarity_threshold = similarity_threshold
#         self.records = []  # 每项为 {'path': ..., 'content': ...}
#
#     def is_similar(self, new_content):
#         for record in self.records:
#             ratio = difflib.SequenceMatcher(None, new_content, record['content']).ratio()
#             if ratio >= self.similarity_threshold:
#                 return record['path'], record['content'], ratio
#         return None
#
#     def add(self, path, content):
#         """
#         添加路径及内容，若内容相似立即提示，若成功添加返回 True。
#         """
#         result = self.is_similar(content)
#         if result:
#             existing_path, existing_content, ratio = result
#             print(f"⚠️ 相似内容（相似度 {ratio:.2f}）: {content}")
#             print(f"   ↪ 已存在路径: {existing_path}，内容: {existing_content}")
#             return False
#         if len(self.records) >= self.max_count:
#             print(f"⚠️ 超过最大记录数 {self.max_count}，未添加: {path}")
#             return False
#         self.records.append({ "path": path, "content": content })
#         print(self.records)
#         return True
#
#     def get_all(self):
#         return self.records
#
#
# if __name__ == "__main__":
#     recorder = UniqueContentRecorder(similarity_threshold=0.85)  # 可调相似度阈值
#     print("请输入路径和内容（输入 q 退出）：")
#     while True:
#         path = input("路径: ").strip()
#         if path.lower() == 'q':
#             break
#         content = input("内容: ").strip()
#         recorder.add(path, content)
#     print("\n✅ 最终唯一记录（按相似度去重）：")
#     for i, item in enumerate(recorder.get_all(), 1):
#         print(f"{i}. 路径: {item['path']}，内容: {item['content']}")

#
# import difflib
#
# class UniqueContentRecorder:
#     def __init__(self, max_count=10000, similarity_threshold=0.9):
#         self.max_count = max_count
#         self.similarity_threshold = similarity_threshold
#         self.records = []  # 每项为 {'path': ..., 'content': ...}
#         self.duplicates = {}  # 原始内容 -> 重复次数
#
#     def is_similar(self, new_content):
#         for i, record in enumerate(self.records):
#             ratio = difflib.SequenceMatcher(None, new_content, record['content']).ratio()
#             if ratio >= self.similarity_threshold:
#                 return i, record['path'], record['content'], ratio
#         return None
#
#     def add(self, path, content):
#         """
#         添加路径及内容，若内容相似立即提示并记录重复次数，若成功添加返回 True。
#         """
#         result = self.is_similar(content)
#         if result:
#             index, existing_path, existing_content, ratio = result
#             self.duplicates[existing_content] = self.duplicates.get(existing_content, 0) + 1
#             print(f"⚠️ 相似内容（相似度 {ratio:.2f}）: {content}")
#             print(f"   ↪ 已存在路径: {existing_path}，内容: {existing_content}")
#             print(f"   🔁 当前重复次数: {self.duplicates[existing_content]}")
#             return False
#         if len(self.records) >= self.max_count:
#             print(f"⚠️ 超过最大记录数 {self.max_count}，未添加: {path}")
#             return False
#         self.records.append({ "path": path, "content": content })
#         print(self.records)
#         print(self.duplicates)
#         return True
#
#     def get_all(self):
#         return self.records
#
#     def get_duplicates(self):
#         """
#         返回所有重复统计结果
#         """
#         return self.duplicates
#
#
# if __name__ == "__main__":
#     recorder = UniqueContentRecorder(similarity_threshold=0.85)
#     print("请输入路径和内容（输入 q 退出）：")
#     while True:
#         path = input("路径: ").strip()
#         if path.lower() == 'q':
#             break
#         content = input("内容: ").strip()
#         recorder.add(path, content)
#
#     print("\n✅ 最终唯一记录（按相似度去重）：")
#     for i, item in enumerate(recorder.get_all(), 1):
#         print(f"{i}. 路径: {item['path']}，内容: {item['content']}")
#
#     print("\n🔁 重复统计：")
#     for content, count in recorder.get_duplicates().items():
#         print(f"内容: {content}，重复次数: {count}")

# import difflib
#
# class UniqueContentRecorder:
#     def __init__(self, max_count=10000, similarity_threshold=0.9):
#         self.max_count = max_count
#         self.similarity_threshold = similarity_threshold
#         self.records = []  # 每项为 {'path': ..., 'content': ..., 'duplicates': int}
#
#     def is_similar(self, new_content):
#         for i, record in enumerate(self.records):
#             ratio = difflib.SequenceMatcher(None, new_content, record['content']).ratio()
#             if ratio >= self.similarity_threshold:
#                 return i, ratio
#         return None
#
#     def add(self, path, content):
#         """
#         添加路径及内容，若内容相似立即提示并+1重复次数，若成功添加返回 True。
#         """
#         result = self.is_similar(content)
#         if result:
#             index, ratio = result
#             self.records[index]['duplicates'] += 1
#             print(f"⚠️ 相似内容（相似度 {ratio:.2f}）: {content}")
#             print(f"   ↪ 已存在路径: {self.records[index]['path']}，内容: {self.records[index]['content']}")
#             print(f"   🔁 当前重复次数: {self.records[index]['duplicates']}")
#             return False
#         if len(self.records) >= self.max_count:
#             print(f"⚠️ 超过最大记录数 {self.max_count}，未添加: {path}")
#             return False
#         self.records.append({
#             "path": path,
#             "content": content,
#             "duplicates": 0
#         })
#         print(self.records)
#         return True
#
#     def get_all(self):
#         return self.records
#
#
# if __name__ == "__main__":
#     recorder = UniqueContentRecorder(similarity_threshold=0.85)
#     print("请输入路径和内容（输入 q 退出）：")
#     while True:
#         path = input("路径: ").strip()
#         if path.lower() == 'q':
#             break
#         content = input("内容: ").strip()
#         recorder.add(path, content)
#
#     print("\n✅ 最终唯一记录（含重复次数）：")
#     for i, item in enumerate(recorder.get_all(), 1):
#         print(f"{i}. {item}")
#
# import difflib
#
# class UniqueContentRecorder:
#     def __init__(self, max_count=10000, similarity_threshold=0.9):
#         self.max_count = max_count
#         self.similarity_threshold = similarity_threshold
#         self.records = []  # 每项为 {'path': ..., 'content': ..., 'duplicates': int, 'similar_contents': [...]}
#
#     def is_similar(self, new_content):
#         for i, record in enumerate(self.records):
#             ratio = difflib.SequenceMatcher(None, new_content, record['content']).ratio()
#             if ratio >= self.similarity_threshold:
#                 return i, ratio
#         return None
#
#     def add(self, path, content):
#         """
#         添加路径及内容，若内容相似，更新记录的重复次数和相似内容列表。
#         """
#         result = self.is_similar(content)
#         if result:
#             index, ratio = result
#             self.records[index]['duplicates'] += 1
#             self.records[index]['similar_contents'].append(content)
#             print(f"⚠️ 相似内容（相似度 {ratio:.2f}）: {content}")
#             print(f"   ↪ 已存在路径: {self.records[index]['path']}，内容: {self.records[index]['content']}")
#             print(f"   🔁 当前重复次数: {self.records[index]['duplicates']}")
#             return False
#         if len(self.records) >= self.max_count:
#             print(f"⚠️ 超过最大记录数 {self.max_count}，未添加: {path}")
#             return False
#         self.records.append({
#             "path": path,
#             "content": content,
#             "duplicates": 0,
#             "similar_contents": []
#         })
#         print(self.records)
#         return True
#
#     def get_all(self):
#         return self.records
#
#
# if __name__ == "__main__":
#     recorder = UniqueContentRecorder(similarity_threshold=0.85)
#     print("请输入路径和内容（输入 q 退出）：")
#     while True:
#         path = input("路径: ").strip()
#         if path.lower() == 'q':
#             break
#         content = input("内容: ").strip()
#         recorder.add(path, content)
#
#     print("\n✅ 最终唯一记录（含重复次数与相似内容）：")
#     for i, item in enumerate(recorder.get_all(), 1):
#         print(f"{i}. {item}")

#
# import difflib
#
# class UniqueContentRecorder:
#     def __init__(self, max_count=10000, similarity_threshold=0.9):
#         self.max_count = max_count
#         self.similarity_threshold = similarity_threshold
#         self.records = []  # 每项为 {'path': ..., 'content': ..., 'duplicates': int, 'similar_contents': [ {title: content} ] }
#
#     def is_similar(self, new_content):
#         for i, record in enumerate(self.records):
#             ratio = difflib.SequenceMatcher(None, new_content, record['content']).ratio()
#             if ratio >= self.similarity_threshold:
#                 return i, ratio
#         return None
#
#     def add(self, path, title, content):
#         """
#         添加路径、标题和内容，若内容相似则增加重复次数并记录标题-内容。
#         """
#         result = self.is_similar(content)
#         if result:
#             index, ratio = result
#             self.records[index]['duplicates'] += 1
#             self.records[index]['similar_contents'].append({title: content})
#             print(f"⚠️ 相似内容（相似度 {ratio:.2f}）: {content}")
#             print(f"   ↪ 已存在路径: {self.records[index]['path']}，内容: {self.records[index]['content']}")
#             print(f"   🔁 当前重复次数: {self.records[index]['duplicates']}")
#             return False
#         if len(self.records) >= self.max_count:
#             print(f"⚠️ 超过最大记录数 {self.max_count}，未添加: {path}")
#             return False
#         self.records.append({
#             "path": path,
#             "content": content,
#             "duplicates": 0,
#             "similar_contents": []
#         })
#
#         print(self.records)
#         return True
#
#     def get_all(self):
#         return self.records
#
#
# if __name__ == "__main__":
#     recorder = UniqueContentRecorder(similarity_threshold=0.85)
#     print("请输入路径、标题和内容（输入 q 退出）：")
#     while True:
#         path = input("路径: ").strip()
#         if path.lower() == 'q':
#             break
#         title = input("标题: ").strip()
#         content = input("内容: ").strip()
#         recorder.add(path, title, content)
#
#     print("\n✅ 最终唯一记录（含重复次数与相似内容）：")
#     for i, item in enumerate(recorder.get_all(), 1):
#         print(f"{i}. {item}")
import difflib

class UniqueContentRecorder:
    def __init__(self, max_count=10000, similarity_threshold=0.9):
        self.max_count = max_count
        self.similarity_threshold = similarity_threshold
        self.records = []  # 每项为 {'id': ..., 'path': ..., 'content': ..., 'duplicates': int, 'similar_contents': [ {title: content} ] }
        self.counter = 1   # 自增 ID 起始值

    def is_similar(self, new_content):
        for i, record in enumerate(self.records):
            ratio = difflib.SequenceMatcher(None, new_content, record['content']).ratio()
            if ratio >= self.similarity_threshold:
                return i, ratio
        return None

    def add(self, path, title, content):
        """
        添加路径、标题和内容。若内容相似，则增加重复次数并记录标题-内容。
        若首次添加，也记录到 similar_contents，并赋予自增 ID。
        """
        result = self.is_similar(content)
        if result:
            index, ratio = result
            self.records[index]['duplicates'] += 1
            self.records[index]['similar_contents'].append({title: content})
            print(f"⚠️ 相似内容（相似度 {ratio:.2f}）: {content}")
            print(f"   ↪ 已存在路径: {self.records[index]['path']}，内容: {self.records[index]['content']}")
            print(f"   🔁 当前重复次数: {self.records[index]['duplicates']}")
            return False

        if len(self.records) >= self.max_count:
            print(f"⚠️ 超过最大记录数 {self.max_count}，未添加: {path}")
            return False

        self.records.append({
            "id": str(self.counter),
            "path": path,
            "content": content,
            "duplicates": 0,
            "similar_contents": [{title: content}]
        })
        self.counter += 1
        print(self.records)
        return True

    def get_all(self):
        return self.records


if __name__ == "__main__":
    recorder = UniqueContentRecorder(similarity_threshold=0.85)
    print("请输入路径、标题和内容（输入 q 退出）：")
    while True:
        path = input("路径: ").strip()
        if path.lower() == 'q':
            break
        title = input("标题: ").strip()
        content = input("内容: ").strip()
        recorder.add(path, title, content)
#     path = "1"
#     title = "EHS保障体系-管理策略和落实"
#     content = '''慧博云通始终坚持走负责任的业务运营之路，积极承担环境和社会责任。安全、健康和环境保护是慧博云通所有运营活动不可分割的部分。慧博云通严格遵守国家和地方相关法律法规标准规范，建立并严格执行公司各项EHS管理程序。同时获得了环境及职业健康管理体系。慧博云通坚持EHS综合管理体系整合运行，通过内、外管理体系的审计使得公司的EHS管理体系日臻完善。保护员工职业健康安全，保护环境和公司设施免受有害影响。
#
# ![图片描述](D:\hbyt\AI智能投标\markdown_output\media\EHS保障体系_v1.0_2211\image0.png)
#
# - 图：慧博云通科技股份有限公司EHS保障体系-管理策略和落实'''
#     recorder.add(path, title, content)

    print("\n✅ 最终唯一记录（含 ID、重复次数与相似内容）：")
    for i, item in enumerate(recorder.get_all(), 1):
        print(f"{i}. {item}")


